<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </a-col>
        <!-- <a-col :span="8" :offset="10">
          <a-input-search
            v-action:search
            style="max-width: 300px; min-width: 150px; float: right"
            v-model="queryParam.search"
            placeholder="请输入课程名字"
            @search="onSearch"
          />
        </a-col> -->
      </a-row>
    </div>
    <s-table ref="table" rowKey="nid" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
       <span slot="card_icon" slot-scope="text">
        <a title="点击查看原图" :href="text" target="_blank">
          <img width="100%" style="max-width: 200px" :src="text" alt="横幅图片" />
        </a>
      </span>
      <div slot="opening_time" slot-scope="text, item">
        <a-tooltip placement="right" arrow-point-at-center>
          <template slot="title">
            {{text}}
          </template>
          <div class="opening_time">{{text}}</div>
        </a-tooltip>
      </div>
       <div slot="limite" slot-scope="text, item">
          <span v-if="item.type == 1">{{limiteText(text)}}次</span>
          <span v-else>不限</span>
      </div>
      <div slot="validity" slot-scope="text, item">
          <span>{{text}}天</span>
      </div>
      <div slot="status" slot-scope="text, item">
          <span>{{statusText(text)}}</span>
      </div>
      <div slot="recommend" slot-scope="text, item">
          <span>{{recommendText(text)}}</span>
      </div>
      <div slot="course_detail" slot-scope="text, item">
        <a-tooltip placement="right" arrow-point-at-center>
          <template slot="title">
            {{text}}
          </template>
          <div class="course_detail">{{text}}</div>
        </a-tooltip>
      </div>
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <template>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </template>
      </div>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/membercard/index'
import { STable } from '@/components'
import AddForm from './modules/AddForm.vue'
import EditForm from './modules/EditForm.vue'

export default {
  name: 'CardList',
  components: {
    STable,
    AddForm,
    EditForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '会员卡ID',
          dataIndex: 'nid',
          align: 'center'
        },
        {
          title: '会员卡icon',
          dataIndex: 'image.preview_url',
          scopedSlots: { customRender: 'card_icon' },
          align: 'center'
        },
        {
          title: '会员卡名称',
          dataIndex: 'name',
          align: 'center'
        },
        {
          title: '简介',
          dataIndex: 'introduce',
          align: 'center'
        },
        {
          title: '使用次数',
          dataIndex: 'limite',
          scopedSlots: { customRender: 'limite' },
          align: 'center'
        },
        {
          title: '有效天数',
          dataIndex: 'effective',
          scopedSlots: { customRender: 'effective' },
          align: 'center'
        },
        {
          title: '使用规则',
          dataIndex: 'use_rules',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '推荐',
          dataIndex: 'recommend',
          scopedSlots: { customRender: 'recommend' },
          align: 'center'
        },
        {
          title: '价格',
          dataIndex: 'price',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center'
        },
        {
          title: '排序',
          dataIndex: 'sort',
          align: 'center'
        },
        {
          title: '添加时间',
          dataIndex: 'create_time',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.all({ ...param, ...this.queryParam })
          .then(response => {
            return response.data
          })
      },
    }
  },
  computed: {
    statusText() {
      const arr = ['下架', '上架']
      return (status) => {
        return arr[status]
      }
    },

    recommendText() {
      const arr = ['否', '是']
      return (status) => {
        return arr[status]
      }
    },

    limiteText() {
      return times => times == 0 ? '不限': times
    }
  },
  created() {},
  methods: {
    /**
     * 新增记录
     */
    handleAdd() {
      this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit(item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
           return Api.del({cardId: item.nid})
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => {
              modal.destroy()
            })
        },
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch() {
      this.handleRefresh(true)
    }
  },
}
</script>

<style lang="less" scoped>
.opening_time, .course_detail{
  overflow : hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
